package com.relytosoft.mizar.media.display.services.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.dubhe.common.log.annotation.OpLog;
import com.relytosoft.dubhe.common.log.enums.OpTypeEnum;
import com.relytosoft.mizar.media.display.api.entity.ViewDetail;
import com.relytosoft.mizar.media.display.api.entity.ViewForm;
import com.relytosoft.mizar.media.display.api.pojo.dto.ViewDetailDto;
import com.relytosoft.mizar.media.display.api.pojo.dto.ViewFormDto;
import com.relytosoft.mizar.media.display.services.service.ViewFormService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 视图资源控制类
 *
 * @author hj
 * @version 1.0
 * @date 2022/4/22 17:48
 * @ClassName ViewFormController
 */
@RestController
@RequestMapping("display/view")
@Api(tags = "视频资源组控制器")
public class ViewFormController {
    @Resource
    private ViewFormService viewFormService;


    @ApiOperation("添加视频组")
    @PostMapping("/insert")
    @SaCheckPermission("display:view:insert")
    @OpLog(msg = "视频资源组添加", opType = OpTypeEnum.INSERT)
    public ResponseResult<Void> insertViewForm(@RequestBody @Valid ViewFormDto dto){
        String ancestor = viewFormService.check(dto);
        ViewForm form = dto.build();
        form.setAncestor(ancestor);
        List<ViewDetailDto> detailDtos = dto.getViewDetails();
        boolean save = viewFormService.insertViewForm(form, detailDtos);
        if (save){
            return ResponseResult.ok();
        }
        return ResponseResult.fail();
    }

    @ApiOperation("查询树")
    @GetMapping("/tree")
    @SaCheckPermission("display:view:tree")
    @OpLog(msg = "查询当前用户视频树",opType = OpTypeEnum.SELECT)
    public ResponseResult<List<Tree<Integer>>> getTree(@RequestParam(required = true) boolean showGroup){
        return ResponseResult.ok(viewFormService.selectTree(showGroup));
    }

    @ApiOperation("修改视频组")
    @PutMapping("/update")
    @SaCheckPermission("display:view:update")
    @OpLog(msg = "视频资源组修改",opType = OpTypeEnum.UPDATE)
    public ResponseResult<Void> updateViewForm(@RequestBody @Valid ViewFormDto dto){
        ViewForm form = dto.build();
        Integer formId = form.getId();
        String ancestor = viewFormService.check(dto);
        form.setAncestor(ancestor);
        List<ViewDetail> viewDetails = dto.getViewDetails()
                .parallelStream().map(m -> m.buildViewDetail(formId))
                .collect(Collectors.toList());
        boolean update = viewFormService.updateViewForm(form, viewDetails);
        if (update){
           return ResponseResult.ok();
        }
        return ResponseResult.fail();
    }

    @ApiOperation("删除视频组")
    @DeleteMapping("{id}/delete")
    @SaCheckPermission("display:view:delete")
    @OpLog(msg = "视频资源组删除",opType = OpTypeEnum.DELETE)
    public ResponseResult<Void> deleteViewForm(@PathVariable @Valid Integer id){
        List<Integer> ids = viewFormService.selectDeleteIds(id);
        boolean delete = viewFormService.deleteById(ids);
        if (delete){
          return   ResponseResult.ok();
        }
        return ResponseResult.fail();
    }

    @ApiOperation("通过名字模糊查询视图")
    @GetMapping("/selectByName")
    @SaCheckPermission("display:view:list")
    @OpLog(msg = "视图名称模糊查询",opType = OpTypeEnum.SELECT)
    public ResponseResult selectByName(String name){
        List<ViewFormDto> viewFormDtos = viewFormService.selectListByName(name);
        return ResponseResult.ok(viewFormDtos);
    }

    @ApiOperation("通过视图id查询码流播放地址")
    @GetMapping("{id}/getPlayUrl")
    @SaCheckPermission("display:view:play")
    @OpLog(msg = "查询码流播放地址",opType = OpTypeEnum.SELECT)
    public ResponseResult getPlayUrl(@PathVariable Integer id){
        return ResponseResult.ok(viewFormService.selectVideoPlay(id));
    }

}
